package com.bjgzy.courselive.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bjgzy.courselive.di.component.DaggerMyCourseComponent;
import com.bjgzy.courselive.mvp.contract.MyCourseContract;
import com.bjgzy.courselive.mvp.presenter.MyCoursePresenter;
import com.bjgzy.courselive.mvp.ui.adapter.MyCourseMultiAdapter;
import com.bjgzy.module_couselive.R;
import com.bjgzy.module_couselive.R2;
import com.blankj.utilcode.util.ObjectUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.MyCourseData;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-1 14:57:37
 */
@Route(path = RouterHub.COURSELIVE_MYCOURSEACTIVITY)
public class MyCourseActivity extends BaseActivity<MyCoursePresenter> implements MyCourseContract.View {
    @Inject
    List<MyCourseData> lists;
    @Inject
    MyCourseMultiAdapter mAdapter;
    @Inject
    LinearLayoutManager mManager;
    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerMyCourseComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        MobclickAgent.onEvent(getActivity(),"ttw_my_course");
        return R.layout.courselive_activity_my_course;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mSwipeLayout.setOnRefreshListener(() -> {
            getDatas(true);
        });
        rvList.setLayoutManager(mManager);
        rvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            MyCourseData myCourseData = mAdapter.getData().get(position);
            if (myCourseData.getItemType() == MyCourseMultiAdapter.ITEM) {
                TextView tv = view.findViewById(R.id.tv_title);
                ARouter.getInstance().build(RouterHub.COURSELIVE_COURSEDETAILSACTIVITY)

                        .withString(Intent.EXTRA_TITLE, tv.getText().toString())
                        .withString(Constants.ROUTER_STRING, RouterHub.COURSELIVE_MYCOURSEACTIVITY)
                        .withString(Constants.ROUTER_IMG, myCourseData.getPreImage())
                        .withInt(Constants.ROUTER_INTEGER, myCourseData.getInternalId())
                        .navigation();
            } else if (myCourseData.getItemType() == MyCourseMultiAdapter.ITEM_AD) {
                ARouter.getInstance().build(RouterHub.COURSELIVE_COURSEDETAILSACTIVITY)

                        .withString(Constants.ROUTER_IMG, myCourseData.getPreImage())
                        .withInt(Constants.ROUTER_INTEGER, myCourseData.getInternalId())
                        .withString(Intent.EXTRA_TITLE, Constants.COURSE_DETAILS)
                        .navigation();
            }
        });
        mAdapter.setOnLoadMoreListener(() -> getDatas(false), rvList);
        mAdapter.loadMoreFail();
        getDatas(true);
    }

    private void getDatas(boolean pullToRefresh) {
        mPresenter.getMyCourseList(pullToRefresh);
    }

    @Override
    public void showLoading() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void setEmptyView() {
        if (ObjectUtils.isNotEmpty(mAdapter) && mAdapter.getEmptyViewCount() <= 0) {
            View view = View.inflate(getActivity(), R.layout.public_custom_empty_layout, null);
            ImageView iv = view.findViewById(R.id.iv_logo);
            iv.setImageResource(R.drawable.no_course);
            QMUIRoundButton btn = view.findViewById(R.id.btn_submit);
            btn.setText("去选课程");
            btn.setOnClickListener((view1) -> {
                killMyself();
            });
            mAdapter.setEmptyView(view);
        }
    }
}
